package com.wangmei.controller;

import com.alipay.api.AlipayApiException;
import com.wangmei.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import unitTool.AlipayBean;
import unitTool.AlipayUtil;
import unitTool.JsonRsp;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/alipay")
public class PayController {
    @Autowired
    private AlipayUtil alipayUtil;

    @Autowired
    private AlipayService alipayService;

    @RequestMapping(value = "/payBeforMoney",method = RequestMethod.POST)
    @ResponseBody
    public JsonRsp alibabaPay(@RequestBody AlipayBean alipayBean){
//        AlipayBean alipayBean = new AlipayBean();
//        alipayBean.setOut_trade_no("oid12345614");
//        alipayBean.setTotal_amount("0.1");
//        alipayBean.setBody("订单详情：无");
//        alipayBean.setSubject("订单名称：无");
        try {
            return JsonRsp.success(alipayUtil.connect(alipayBean));
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return JsonRsp.fail(100,"error");
        }
    }

    @RequestMapping(value = "/payResult")
    public String payResult(HttpSession session,
                            @RequestParam("out_trade_no")String oid){
        //接收参数，修改数据库
        alipayService.paySuccess(oid);
        return "redirect:../shop/index.html";
    }

}
